package com.hanZhe.controller;

import com.hanZhe.bean.Users;
import com.hanZhe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by shw on 2018/1/5.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/regist.do")
    public ModelAndView regist(Users users){
        System.out.println("前端接收到的数据"+users);
        ModelAndView modelAndView=new ModelAndView();
        int insert = userService.insert(users);
        if(insert>0){
            modelAndView.setViewName("login");
            modelAndView.addObject("result","注册成功");
        }else {
            modelAndView.setViewName("regist");
            modelAndView.addObject("result","注册失败，重新注册");
        }

        return modelAndView;
    }

    @RequestMapping("/login.do")
    public ModelAndView login(Users users){
        System.out.println("前端接收到的数据"+users);
        ModelAndView modelAndView=new ModelAndView();

        Users users1 = userService.selectByUser(users);
        if(users1!=null){
            modelAndView.setViewName("index");
            modelAndView.addObject("result","登陆成功");
        }else {
            modelAndView.setViewName("login");
            modelAndView.addObject("result","登录失败，重新登陆");
        }
        return modelAndView;
    }
}
